package com.xfly.app.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "WXPayEntryActivity";
	
    private IWXAPI api;

	private static PayResultListener mPayResultListener;

	public static void setPayResultListener(PayResultListener listener){
		mPayResultListener = listener;
	}

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, "wx971b735e6872c3b7");
    	api.registerApp("wx971b735e6872c3b7");
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode+","+resp.errStr);
		int code = resp.errCode;
		/*
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("result");
		builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));

		builder.create().show();
		*/
		if (code == 0) {//支付完成
			mPayResultListener.onSuccess("支付成功");
			this.finish();
		} else if (code == -1) {//支付失败
			Toast.makeText(getApplicationContext(),"支付失败",Toast.LENGTH_SHORT).show();
			mPayResultListener.onFail("支付失败");
			this.finish();
		} else if (code == -2) {//用户取消
			Toast.makeText(this,"用户取消",Toast.LENGTH_SHORT).show();
			mPayResultListener.onFail("用户取消");
			this.finish();
		}
	}
}
